package com.longrise.common.utils;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialog;
import android.widget.Toast;

import com.longrise.common.R;
import com.longrise.common.base.BaseActivity;

public abstract class AppBaseDialog extends AppCompatDialog {

    /**
     * 默认为InPutDialog style
     */
    public AppBaseDialog(Context context) {
        this(context, R.style.InPutDialog);
        setCanceledOnTouchOutside(false);
    }

    /**
     * 如果需要其他style 请使用该构造函数
     */
    public AppBaseDialog(Context context, int style) {
        super(context, style);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId(savedInstanceState));

        init();
        regEvent();
    }

    public abstract int getLayoutResourceId(Bundle bundle);

    public abstract void init();

    public abstract void regEvent();

    protected void showToast(String msg) {
        if (getContext() instanceof BaseActivity) {
            BaseActivity bAct = (BaseActivity) getContext();
            bAct.showToast(msg);
        } else {
            //兼容未extends BaseActivity
            DZZWTools.showToast(getContext(), msg, Toast.LENGTH_SHORT);
        }
    }

    protected boolean checkNetworkEnable() {
        if (getContext() instanceof BaseActivity) {
            BaseActivity bAct = (BaseActivity) getContext();
            return bAct.checkNetWorkEnable();
        } else {
            //兼容未extends BaseActivity
            boolean enable = NetUtil.checkNetEnable();
            return enable;
        }
    }
}
